/*--------------------------------------------------------------------------------*/
#ifndef __GRAPHIC_2D_ANIMATED_OBJECT__
#define __GRAPHIC_2D_ANIMATED_OBJECT__
/*--------------------------------------------------------------------------------*/
#include "primitives.h"
#include "object.h"
#include "util.h"
#include "graphic_animation_data.h"
#include "graphic_vertex_buffer.h"
#include "graphic_2d_object.h"
#include "math_angle.h"
/*--------------------------------------------------------------------------------*/
class GRAPHIC_2D_ANIMATED_OBJECT : public GRAPHIC_2D_OBJECT
{

	// -- PUBLIC

public:

    DECLARE_REF( GRAPHIC_2D_ANIMATED_OBJECT );

    // .. CONSTRUCTORS

    GRAPHIC_2D_ANIMATED_OBJECT() : GRAPHIC_2D_OBJECT(), Animation(), CurrentTime( 0.0f )
    {
		VertexTable.SetItemCount( 6 );
    }

    virtual ~GRAPHIC_2D_ANIMATED_OBJECT()
    {
    }

    // .. METHODS

    VOID SetAnimation( const GRAPHIC_ANIMATION_DATA & animation )
    {
    	Animation = & animation;
    }

    VOID SetAnimation( const GRAPHIC_ANIMATION_DATA::REF & animation )
	{
		Animation = animation;
	}

    virtual VOID ComputeVertexTable()
    {
    	MATH_VECTOR_2
			half_extent( Extent * 0.5f );
    	REAL
			sinus = MATH_GetSinus( Angle ),
			cosinus = MATH_GetCosinus( Angle );

    	DEBUG_Require( !Animation.IsNull() );

    	const GRAPHIC_TEXTURE_SET::UV_COORD
			coord = Animation->GetUVCoord( CurrentTime );

		// Triangle 1
		VertexTable[ 0 ].Position.X = Position.X + cosinus * -half_extent.X + sinus * half_extent.Y;
		VertexTable[ 0 ].Position.Y = Position.Y + -sinus * -half_extent.X + cosinus * half_extent.Y;
		VertexTable[ 1 ].Position.X = Position.X + cosinus * half_extent.X + sinus * half_extent.Y;
		VertexTable[ 1 ].Position.Y = Position.Y + -sinus * half_extent.X + cosinus * half_extent.Y;
		VertexTable[ 2 ].Position.X = Position.X + cosinus * half_extent.X + sinus * -half_extent.Y;
		VertexTable[ 2 ].Position.Y = Position.Y + -sinus * half_extent.X + cosinus * -half_extent.Y;
		VertexTable[ 0 ].UV.Set( coord.Start.X, coord.End.Y );
		VertexTable[ 1 ].UV.Set( coord.End.X, coord.End.Y );
		VertexTable[ 2 ].UV.Set( coord.End.X, coord.Start.Y );


		// Triangle 2
		VertexTable[ 3 ].Position = VertexTable[2].Position;
		VertexTable[ 4 ].Position.X = Position.X + cosinus * -half_extent.X + sinus * -half_extent.Y;
		VertexTable[ 4 ].Position.Y = Position.Y + -sinus * -half_extent.X + cosinus * -half_extent.Y;
		VertexTable[ 5 ].Position = VertexTable[0].Position;
		VertexTable[ 3 ].UV = VertexTable[2].UV;
		VertexTable[ 4 ].UV.Set( coord.Start.X, coord.Start.Y );
		VertexTable[ 5 ].UV = VertexTable[0].UV;

		// :TODO: sin & cos to apply rotation
    }

    virtual VOID Update( const TIMER & timer )
    {
    	CurrentTime += timer.GetSecondCount();

    	while( CurrentTime >= Animation->GetDuration() )
    	{
			CurrentTime = CurrentTime - Animation->GetDuration();
    	}

    	ComputeVertexTable();
    }

    virtual VOID Render()
    {
    	OPENGL_Color( Color );
    	Animation->GetTextureSetSource().BindTexture();
    	GRAPHIC_VERTEX_BUFFER::EnableVertexArray( ( REAL * ) VertexTable.GetArray(), 2, sizeof( VERTEX ) );
    	GRAPHIC_VERTEX_BUFFER::EnableTexCoordArray( ( REAL * ) & VertexTable[0].UV , 2, sizeof( VERTEX ) );
    	GRAPHIC_VERTEX_BUFFER::Draw( VertexTable.GetItemCount() );
    	DEBUG_CheckOpenGL();
    }

    // -- PRIVATE

private :

    // .. ATTRIBUTES

    GRAPHIC_ANIMATION_DATA::REF
		Animation;
    REAL
		CurrentTime;

    
};
/*--------------------------------------------------------------------------------*/
#endif
